﻿using System.Application;
using System.Domain.Entity;
using Microsoft.AspNetCore.Mvc;
using Ubiquity.Core.Extend;
using Ubiquity.Core.Web;
using Ubiquity.Web.Filters;

namespace Ubiquity.Web.Areas.SystemManage.Controllers
{
    /// <summary>
    ///数据库备份 控制器 
    /// </summary>	

    [Area("SystemManage")]
    public class SysDbBackupController : BaseController
    {
        private readonly SysDbBackupService _service;

        public SysDbBackupController(SysDbBackupService service)
        { 
            _service = service;
        }

        [HttpGet]
        [Authorize]
        public ActionResult GetGridPaginationJson(WebPagination pagination)
        {
            var paging = pagination.Change();
            var data = new
            {
                rows = _service.GetList(paging),
                pagination.total,
                page = pagination.page ,
                pagination.records
            };
            return Content(data.ToJson());
        }


        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult Insert(SysDbBackupEntity entity, string keyValue)
        {
            // TODO 待处理
            //string directoryPath = Server.MapPath("~/Resource/DbBackup/");
            // FileHelper.CreateDirectory(directoryPath);
            //entity.FilePath = directoryPath + entity.FileName + ".bak";
            entity.FileName = entity.FileName + ".bak";
            return _service.Insert(entity) ? Success("操作成功。") : Error("操作失败。");
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            return _service.Delete(keyValue) ? Success("操作成功。") : Error("操作失败。");
        }
        [HttpPost]
        public void DownloadBackup(string keyValue)
        {
            // TODO 待处理
            //var data = _app.GetEntity(keyValue);
            //string filename = Server.UrlDecode(data.FileName);
            //string filepath = Server.MapPath(data.FilePath);
            //if (FileDownHelper.FileExists(filepath))
            //{
            //    FileDownHelper.DownLoadold(filepath, filename);
            //}
        }
    }
}
